/*	This file is part of LSAC.

	LSAC is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	LSAC is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with LSAC.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (c) 2010 LEPESME "Jiboo" Jean-Baptiste
*/

#include "cInstReturn.h"
#include "lsac.h"

cInstReturn::cInstReturn(cLocation pLocation, cBlock *pBlock, cExpression *pExpression)
	: cInstruction(pLocation, pBlock, "")
{
	this->dExpression = pExpression;
}

cInstReturn::~cInstReturn()
{
	delete this->dExpression;
}

void cInstReturn::print(int pLevel)
{
	PRINT_LOG("cInstReturn " << this);
	PRINT_LOG("{");
	this->dExpression->print(pLevel + 1);
	PRINT_LOG("}");
}

void cInstReturn::validate(cSemContext *pContext)
{
	if(typeid(*(this->dParent)) != typeid(cFunction)) ERROR("Erreur en " << this->dLocation << " l' instruction retourne ne peut etre utilise qu' a la racine d' une fonction.");
	if(this->dExpression->getType(pContext) != ((cFunction*)this->dParent)->dType)
		ERROR("Erreur en " << this->dLocation << " l' expression retourner a un type different de la fonction");
}

void cInstReturn::genCode(ostream &pStream)
{
	pStream << "return ";
	this->dExpression->genCode(pStream);
	pStream << ";" << endl;

}
